/**
 * Copyright (c) 2009  Collaborative Development Group, C.S. Dept., University of Bari
 *
 * All rights reserved. This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0  which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 */
package it.uniba.di.cdg.jtalk.ui.login;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import it.uniba.di.cdg.jtalk.core.IPreferenceManager;
import it.uniba.di.cdg.jtalk.core.IUserSession;
import it.uniba.di.cdg.jtalk.core.LoginFailedException;
import it.uniba.di.cdg.jtalk.internal.ApplicationFacade;
import it.uniba.di.cdg.jtalk.ui.ChatPerspective;
import it.uniba.di.cdg.penelope.presenterfirst.SUT;
import it.uniba.di.cdg.penelope.ui.lnf.IEclipseUIHelper;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.osgi.service.log.LogService;

import com.google.inject.Provider;

/**
 * Behavior tests for {@link ApplicationFacade} implementation.
 */
@RunWith( MockitoJUnitRunner.class ) 
public class ApplicationFacadeTest {

	private static final String USER_ID = "dummy";
	private static final String PASSWORD = "dummy";

	@SUT
	private ApplicationFacade applicationFacade;
	
	@Mock
	private Provider<IUserSession> sessionProvider;
	@Mock
	private IUserSession userSession;
	@Mock
	private LogService logger;
	@Mock
	private IEclipseUIHelper eclipseUIHelper;
	@Mock
	private IPreferenceManager preferenceManager;
	
	@Before
	public void setUpFacade() {
		when( sessionProvider.get() ).thenReturn( userSession );
		applicationFacade = new ApplicationFacade( sessionProvider, logger, eclipseUIHelper, preferenceManager );
	}
	
	@Test
	public void whenSuccesfulLogInSwitchToChatPerspective() {
		//given that there is no actual session and login will succeeds
		doNothing().when( userSession ).logIn( anyString(), anyString() );

		//when login is attempted
		applicationFacade.logIn( USER_ID, PASSWORD, false, false );
		
		//then perspective is switched and preferences stored!
		verify( eclipseUIHelper ).switchPerspective( ChatPerspective.PERSPECTIVE_ID );
		verify( preferenceManager ).setAutomaticLogin( false );
		verify( preferenceManager ).setRememberPassword( false );
	}
	
	@Test
	public void whenLogInFailsRemainOnTheCurrentPerspective() {
		//given that login is going to fail
		doThrow( new LoginFailedException( "Login has failed!" ))
			.when( userSession ).logIn( anyString(), anyString() );
		
		//when login is attempted
		try {
			applicationFacade.logIn( USER_ID, PASSWORD, false, false );
		} catch( Throwable t) { /* Silence since we want to check behavior */ }
		
		//then remain on the same perspective and view
		verify( eclipseUIHelper, never() ).switchPerspective( anyString() );
		verify( eclipseUIHelper, never() ).showView( anyString() );
		verify( eclipseUIHelper, never() ).closePerspective( anyString() );
		verifyZeroInteractions( preferenceManager );
	}
	
	@Test
	public void whenLogOffAndNotConnectedDoNothing() {
		//given a not connected application
		
		//when loggin off requested
		applicationFacade.logOff();
		
		//then do nothing
		verify( userSession, never() ).logOff();
	}
}
